
//显示图书
function showBooks()
{
    $.ajax({
        type: "GET",
        headers : {authorization:header},
        dataType: "json",
        async: true,
        url: "http://120.79.183.142/api/manageBooks/bookList",
        success:function (result) {
            var liuzhi_data = result["data"];
            //console.log(liuzhi_data);
            var num = liuzhi_data.length;
            for (var index2 = 0; index2 < num; index2++) {
                var irow = liuzhi_data[index2];
                var bid = irow["bid"];
                var category = irow["cname"];
                var name = irow["name"];
                var author = irow["author"];
                var publisher = irow["publisher"];
                var total_num = irow["total_num"];
                var remain_num = irow["remain_num"];

                var str = '<tr id="tr_' + bid + '">' + '<td>' + bid + '</td><td>' + category + '</td><td>' + name + '</td><td>' + author + '</td><td>' + publisher + '</td><td id=\"total'+bid+'\">'
                    + total_num + '</td><td id=\"remain'+bid+'\">' + remain_num + '</td>' +  '<td><button class="btn btn-success btn-xs" data-toggle="modal" data-target="#reviseBook" onclick="addNumOfBook1(this)">增加</button>' +
                    '<button class="btn btn-danger btn-xs" data-toggle="modal" data-target="#deleteBook" onclick="subtractNumOfBook1(this)">删除</button></td></tr>';

                // console.log(str);
                $("#ma tbody:last").append(str);
            }
            parametersOfDataTable();
        },
        error:function (r) {
            console.log(r);

        }
    })
}

//DataTable的参数
function parametersOfDataTable(){
    $("#ma").DataTable({
        destroy:true,
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }

    });
}

//新增图书种类
function  addTypeOfBook() {
    console.log($("#buyForm").serialize());
    $.ajax({
        type: "POST",
        headers : {authorization:header},
        dataType: "json",
        async: true,
        url: "http://120.79.183.142/api/manageBooks/addBooks",
        data: $("#buyForm").serialize(),
        success:function (data) {
            if(data.code === 200)
            {
                var bid = data.data;
                var category = document.getElementById("category").value;
                var name = document.getElementById("bname").value;
                var author = document.getElementById("author").value;
                var publisher = document.getElementById("press").value;
                var total_num = document.getElementById("total-number").value;
                var remain_num = total_num;

                var str = '<tr id="tr_' + bid + '">' + '<td>' + bid + '</td><td>' + category + '</td><td>' + name + '</td><td>' + author + '</td><td>' + publisher + '</td><td id=\"total'+bid+'\">'
                    + total_num + '</td><td id=\"remain'+bid+'\">' + remain_num + '</td>' +  '<td><button class="btn btn-success btn-xs" data-toggle="modal" data-target="#reviseBook" onclick="addNumOfBook1(this)">增加</button>' +
                    '<button class="btn btn-danger btn-xs" data-toggle="modal" data-target="#deleteBook" onclick="subtractOfBook1(this)">删除</button></td></tr>';

                $("#ma tbody:last").append(str);

                document.getElementById("cancel_buy").click();
                $("#success").css('opacity',100);
                setTimeout(function () {
                    $("#success").css('opacity',0);
                },2500);
            }
        },
        error:function (r) {
            $("#fail").css('opacity',100);
            setTimeout(function () {
                $("#fail").css('opacity',0);
            },2500);
        }
    })
}


//增加图书数量
var liuzhi2_bid;
function addNumOfBook1(button) {
    liuzhi2_bid = button.parentNode.parentNode.children[0].innerHTML;
}
function addNumOfBook2() {
    var liuzhi_2number = $("#addNumOfBook").val();
    $.ajax({
        type: "POST",
        headers : {authorization:header},
        dataType: "json",
        async: true,
        url: "http://120.79.183.142/api/manageBooks/increaseBooks",
        data: {"bid":liuzhi2_bid, "number":liuzhi_2number},
        success:function (data) {
            if(data.code === 200)
            {
                document.getElementById('total'+liuzhi2_bid).innerHTML=parseInt(document.getElementById('total'+liuzhi2_bid).innerHTML)+parseInt(liuzhi_2number);
                document.getElementById('remain'+liuzhi2_bid).innerHTML=parseInt(document.getElementById('remain'+liuzhi2_bid).innerHTML)+parseInt(liuzhi_2number);
                document.getElementById('cancel_add').click();
                $("#success").css('opacity',100);
                setTimeout(function () {
                    $("#success").css('opacity',0);
                },2500);

            }
            else{
                $("#fail").css('opacity',100);
                setTimeout(function () {
                    $("#fail").css('opacity',0);
                },2500);
            }
        },
        error:function (r) {
            $("#fail").css('opacity',100);
            setTimeout(function () {
                $("#fail").css('opacity',0);
            },2500);
        }
    })
}


//减少图书数量
var liuzhi_bid;
var liuzhi_number;
function subtractNumOfBook1(button) {
    liuzhi_bid = button.parentNode.parentNode.children[0].innerHTML;
}
function  subtractNumOfBook2() {
    liuzhi_number = $("#book_delete").val();
    console.log(liuzhi_number);

    $.ajax({
        type: "POST",
        headers : {authorization:header},
        dataType: "json",
        async: true,
        url: "http://120.79.183.142/api/manageBooks/deleteBooks",
        data: {"bid":liuzhi_bid, "number":liuzhi_number},
        success:function (data) {
            if(data.code === 200)
            {
                //location.reload();
                document.getElementById('total'+liuzhi_bid).innerHTML=parseInt(document.getElementById('total'+liuzhi_bid).innerHTML)-parseInt(liuzhi_number);
                document.getElementById('remain'+liuzhi_bid).innerHTML=parseInt(document.getElementById('remain'+liuzhi_bid).innerHTML)-parseInt(liuzhi_number);
                document.getElementById('cancel_delete').click();

                if(parseInt(document.getElementById('total'+liuzhi_bid).innerHTML) === 0)
                {
                    $("#tr_" + liuzhi_bid).remove();
                }
                $("#success").css('opacity',100);
                setTimeout(function () {
                    $("#success").css('opacity',0);
                },2500);
            }
            else{
                $("#fail").css('opacity',100);
                setTimeout(function () {
                    $("#fail").css('opacity',0);
                },2500);
            }
        },
        error:function (r) {
            $("#fail").css('opacity',100);
            setTimeout(function () {
                $("#fail").css('opacity',0);
            },2500);
        }
    })
}

//目录列表
function catalogList(){

    $.ajax({
        type: "GET",
        headers : {authorization:header},
        dataType:"json",
        async: true,
        url: "http://120.79.183.142/api/manageBooks/categoryList",
        success: function (result) {
            if(result.code === 200)
            {
                var liuzhi_list_data = result["data"];
                var liuzhi_list_num = liuzhi_list_data.length;
                for(var liuzhi_i=0; liuzhi_i<liuzhi_list_num; liuzhi_i++)
                {
                    var liuzhi_irow = liuzhi_list_data[liuzhi_i];
                    var name = liuzhi_irow["name"];
                    var cid = liuzhi_irow["cid"];

                    var str = '<tr>' + '<td>' + name + '</td><td id=\"' + cid + '\">' + cid + '</td>' + '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target="#category_list" onclick="goToList('+cid+')" ">详情</button></td></tr>';
                    $("#list_ma tbody:last").append(str);
                }

            }
            else
            {
                alert(data.data);
                location.reload();
            }

            list_dt();
        },
        error: function (r) {
            alert(r);
            location.reload();
        }
    })
}

function  goToList(cid) {
    $.ajax({
        type: "POST",
        headers : {authorization:header},
        dataType:"json",
        async: true,
        url: "http://120.79.183.142/api/manageBooks/categoryDetail?cid="+cid,
        success: function (result) {
            if(result.code === 200)
            {
                console.log(result)
                var liuzhi_list_data = result["data"];
                var liuzhi_list_num = liuzhi_list_data.length;
                console.log($('#category_content'))
                $('#category_content').remove();

                for(var liuzhi_i=0; liuzhi_i<liuzhi_list_num; liuzhi_i++)
                {
                    var liuzhi_irow = liuzhi_list_data[liuzhi_i];
                    var name = liuzhi_irow["name"];
                    var bid = liuzhi_irow["bid"];
                    var author=liuzhi_irow["author"];
                    var publisher=liuzhi_irow["publisher"];

                    var str='<tr id="#category_content"><td>'+bid+'</td><td>'+name+'</td><td>'+author+'</td><td>'+publisher+'</td><td>'+'<button class="btn btn-danger btn-xs" data-toggle="modal" data-target="#category_list">详情</button>'+'</td></tr>'
                    console.log(str)
                    $("#category_dt tbody:last").append(str);
                }
            }
            else
            {
                alert(data.data);
                location.reload();
            }
            category_dt();
        },
        error: function (r) {
            alert(r);
            location.reload();
        }
    })
}

//dataTable参数
function category_dt() {
    $("#category_dt").DataTable({
        destroy: true,
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }
    });

}

//dataTable参数
function list_dt() {
    $("#list_ma").DataTable({
        destroy: true,
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }

    });
}

//增加目录
function addList() {
    console.log($("#listForm").serialize());
    $.ajax({
        type: "POST",
        headers : {authorization:header},
        dataType: "json",
        async: true,
        url: "http://120.79.183.142/api/manageBooks/addCategory",
        data: $("#listForm").serialize(),
        success:function (data) {
            if(data.code === 200)
            {
                //location.reload();
                //console.log(1111);
                var cid = data.data;
                var name = document.getElementById("list_name").value;
                var str = '<tr>' + '<td>' + name + '</td><td id=\"' + cid + '\">' + cid + '</td>' + '<td><button class="btn btn-danger btn-xs" data-toggle="modal" onClick="location.href=\'list.html?cid=' + cid + '\'\">详情</button></td></tr>';
                $("#list_ma tbody:last").append(str);
                document.getElementById("cancel_list").click();
                $("#success").css('opacity',100);
                setTimeout(function () {
                    $("#success").css('opacity',0);
                },2500);
            }
        },
        error:function (r) {
            $("#fail").css('opacity',100);
            setTimeout(function () {
                $("#fail").css('opacity',0);
            },2500);        }
    })
}
